// import breadcrumb from '../../common/breadcrumb'

import {getNeedDealList,getProcInsTrace,getNextApprovePerson,getWFConfigActivity,getBackNode,
    getBpmPscActInfo} from '@/api/api.js'
import * as Constant from '@/utils/constant'
import api_home from '@/api/api_home'
export default {
    components:{
        // breadcrumb
    },
    data(){
        return {
            brand:[
                {name: 'lang.workbench'},
                {name: 'lang.need_deal_task'}
            ],
            hasIcon:true,
            tables:[

            ],
            keysword:'',//关键字
            currentPage: 1,
            total:0,
            pageSize:10,
            applicate_time:'',
            advSearch: 'ndt.advance_search',
            userData:[
            ],
            taskDes:'',
            startTime:'',
            searchParams:{},
            procPicUrl:process.env.PROCPIC_ROOT,

            actCache:[],
            nextApproPeopleCaceh:[],
            backActCache:[],
            processRouterMap: {
                "SWP-SU\\pur_demIn": {
                  "020_队办经理审批": Constant.ROUTE_PUR_PROCESS_DEM_WITHIN_NEXT.index,
                  "030_计划协调员审批": Constant.ROUTE_PUR_PROCESS_DEM_WITHIN_NEXT.index,
                  "040_调试采购主管审批": Constant.ROUTE_PUR_PROCESS_DEM_WITHIN_NEXT.index
                },
                "SWP-SU\\pur_demOut": {
                  "020_队办经理审批": Constant.ROUTE_PUR_PROCESS_DEM_WITHOUT_NEXT.index,
                  "030_计划协调员审批": Constant.ROUTE_PUR_PROCESS_DEM_WITHOUT_NEXT.index,
                  "040_调试采购主管审批": Constant.ROUTE_PUR_PROCESS_DEM_WITHOUT_NEXT.index
                },
                "SWP-SU\\pur_demChange": {
                  "020_队办经理审批": Constant.ROUTE_PUR_PROCESS_DEM_CHANGE_NEXT.index,
                  "030_计划协调员审批": Constant.ROUTE_PUR_PROCESS_DEM_CHANGE_NEXT.index,
                  "040_调试采购主管审批": Constant.ROUTE_PUR_PROCESS_DEM_CHANGE_NEXT.index
                },
                "SWP-SU\\pur_demClose": {
                  "020_计划协调员审批": Constant.ROUTE_PUR_PROCESS_DEM_CLOSE_NEXT.index,
                  "030_调试采购主管审批": Constant.ROUTE_PUR_PROCESS_DEM_CLOSE_NEXT.index,
                  "040_调试采购主管审批": Constant.ROUTE_PUR_PROCESS_DEM_CLOSE_NEXT.index
                },
                "SWP-SU\\sci_thesis": {
                    "010_提交论文": Constant.ROUTE_SRI_GAIN_THESIS_APPROVE.index,
                    "020_论文管理员审批": Constant.ROUTE_SRI_GAIN_THESIS_APPROVE.index
                },
                "SWP-SU\\sci_budget": {
                    "010_提交预算申请": Constant.ROUTE_SRI_BUDGET_APPROVE.index,
                    "020_预算管理员审批": Constant.ROUTE_SRI_BUDGET_APPROVE.index
                },
                "SWP-SU\\sci_cbpPlan": {
                    "010_提交能力建设年度工作计划": Constant.ROUTE_SRI_GAIN_CBP_PLAN_APPROVE.index,
                    "020_专业分部协调员审批": Constant.ROUTE_SRI_GAIN_CBP_PLAN_APPROVE.index,
                    "030_能力建设管理工程师审批": Constant.ROUTE_SRI_GAIN_CBP_PLAN_APPROVE.index,
                    "040_主任或副主任审批": Constant.ROUTE_SRI_GAIN_CBP_PLAN_APPROVE.index,
                },
                "SWP-SU\\sci_cbpProcess" :{
                    "010_项目负责人提交能建计划进展": Constant.ROUTE_SRI_GAIN_CBP_PROGRESS_APPROVE.index,
                    "020_能力建设管理工程师审批": Constant.ROUTE_SRI_GAIN_CBP_PROGRESS_APPROVE.index,
                },
                "SWP-SU\\sri_alpPlan" :{
                    "010_提交实验室年度工作计划": Constant.ROUTE_SRI_GAIN_ALP_PLAN_APPROVE.index,
                    "020_实验室专家": Constant.ROUTE_SRI_GAIN_ALP_PLAN_APPROVE.index,
                    "030_主任或副主任审批": Constant.ROUTE_SRI_GAIN_ALP_PLAN_APPROVE.index,
                },
                "SWP-SU\\sri_alpProcess" :{
                    "010_项目负责人提交工作进展": Constant.ROUTE_SRI_GAIN_ALP_PROGRESS_APPROVE.index,
                    "020_实验室专家": Constant.ROUTE_SRI_GAIN_ALP_PROGRESS_APPROVE.index,
                    "030_主任或副主任审批": Constant.ROUTE_SRI_GAIN_ALP_PROGRESS_APPROVE.index,
                },
                "SWP-SU\\hr_backApproval":{
                    "010_编制人员调配计划": Constant.ROUTE_HR_BACK_APPROVE.index,
                    "020_队办经理审批": Constant.ROUTE_HR_BACK_APPROVE.index,
                    "030_SAM归档": Constant.ROUTE_HR_BACK_APPROVE.index,
                },
                "SWP-SU\\hr_frontApproval":{
                    "010_编制人员岗位需求": Constant.ROUTE_HR_FONT_APPROVE.index,
                    "020_队办经理审批": Constant.ROUTE_HR_FONT_APPROVE.index,
                    "030_分部经理审批": Constant.ROUTE_HR_FONT_APPROVE.index,
                    "040_专业工程师审批": Constant.ROUTE_HR_FONT_APPROVE.index,
                    "050_专业部门处长审批": Constant.ROUTE_HR_FONT_APPROVE.index,
                    "060_SPB成本审批": Constant.ROUTE_HR_FONT_APPROVE.index,
                    "070_SMB归档": Constant.ROUTE_HR_FONT_APPROVE.index,
                },
            }
        }
    },
    methods:{
        //高级搜索展开与收缩
        advanceSearch : function(){
            if(this.advSearch == "ndt.advance_search"){
                this.advSearch = "ndt.return";
            }else{
                this.advSearch = "ndt.advance_search";
            }
        },
        /**
         * 查询待办
         * @param {*} params
         */
        getNeedDealList(params){
            let _this = this;
            getNeedDealList(params).then((result) => {
                _this.tables = [];
                if(result.status == "200" && result.data.code == "0"){
                    let data = result.data.data.data;
                    let dataList = data.ToDoTasks;
                    let pageObj = data.Paging;
                    for(let i = 0; i < dataList.length; i++){
                        dataList[i].deal_man = [];
                        dataList[i].determinate = "1";//设置对下一流程是否同意
                        dataList[i].isHasReadTask = false;
                    }
                    //填充表格
                    _this.tables.push(...dataList);
                    //分页数据
                    _this.currentPage = pageObj.startPage;
                    _this.pageSize = pageObj.pageSize;
                    _this.total = pageObj.totalCount;
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }

            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });

        },
        //普通搜索
        commonSearch:function(){
            let params = {
                pageIndex: this.currentPage,
                pageSize: this.pageSize
            }
            this.searchParams = {
                taskDesc: this.keysword
            }
            params = Object.assign(params,this.searchParams);
            this.getNeedDealList(params);
        },
        //高级搜索
        search:function(){
            let params = {
                pageIndex: this.currentPage,
                pageSize: this.pageSize
            }
            this.searchParams = {
                taskDesc: this.taskDes,
                launchTime: this.startTime
            }
            params = Object.assign(params,this.searchParams);
            this.getNeedDealList(params);
        },
        //重置数据 （高级搜索的数据）
        resetData:function(){
            this.taskDes = "";
            this.startTime  = null;
        },
        //改变每页显示条数
        changeSize:function(pageSize){
            let params = {
                pageIndex:this.currentPage,
                pageSize:this.pageSize = pageSize,
            }
            params = Object.assign(params,this.searchParams);
            this.getNeedDealList(params);
        },
        //改变当前页数
        changeCurrentPage:function(nowPage){
            let params = {
                pageIndex:this.currentPage = nowPage,
                pageSize:this.pageSize ,
            }
            params = Object.assign(params,this.searchParams);
            this.getNeedDealList(params);
        },
        //上一页
        prePage:function(){
            let params = {
                pageIndex:this.currentPage = this.currentPage - 1 ,
                pageSize:this.pageSize ,
            }
            params = Object.assign(params,this.searchParams);
            this.getNeedDealList(params);
        },
        //下一页
        nextPage:function(){
            let params = {
                pageIndex:this.currentPage = this.currentPage + 1 ,
                pageSize:this.pageSize ,
            }
            params = Object.assign(params,this.searchParams);
            this.getNeedDealList(params);

        },
        /**
         * 编辑进入流程详情
         * @param {*} item
         */
        editProcInst:function(item){
          console.log("异常调试信息===216==2.js==："+item)
            item.showProcLogs = [];//流程日志
            item.actOptions = [];
            item.nextActOptions = [];//下一环节选项
            item.isEnd = false;//是否为结束节点 默认false 下一环节处理
            item.personOptions = [];//某个环节的审批人
            item.approvePerson = [];//审批人数组
            //查询流程日志
            this.showProcLogAndLastaMan(item);
            //获取下一步点处理人 有下一环节020 和 相对应的处理人 数组形式
            this.getNextDealInfo(item);
            let _this = this;
            if(this.processRouterMap[item.processFullName][item.activityName]) {
                api_home.updateVisitTime({processFullName: item.processFullName, processName: item.processName}, response => {
                })
                setTimeout(()=>{
                  _this.$router.push({path:this.processRouterMap[item.processFullName][item.activityName],query:{obj:item}});
                }, 2000)
            }else{
                _this.$alert("please check processRouterMap.");
            }

            // setTimeout(function(){
            //     _this.$router.push({path:"/need_deal_task_details/"+item.procInstID,query:{obj:item}});
            // },1000);

        },
        deleteProcInst:function(procInstId){

        },
        /**
         * 查询该条待办的流程轨迹
         * @param {待办数据} item
         */
        showProcLogAndLastaMan(item){
            let _this = this;
            let params = {
                procInsId: item.procInstID,
                pageIndex: 1,
                pageSize: 1000,
            };
            getProcInsTrace(params).then((result) => {
                item.showProcLogs = [];
                if(result.status == "200" && result.data.code == "0"){
                    let data = result.data.data.data;
                    for(let i = 0; i < data.ProcessTraces.length; i++){
                        item.showProcLogs.push(data.ProcessTraces[i].processLogDTO);
                    }
                    //判断是否有回退信息
                    _this.isBackNode(item);
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        /**
         * 判断是否有回退节点
         * @param {待办数据} item
         */
        isBackNode(item){
            //判断是否有回退按钮
            let procId = item.procID;
            let actId = (item.activityName.split('_'))[0];
            let _this = this;
            item.showBack = false;
            item.backAct = [];
            item.backActName = [];
            item.backMan = [];
            item.backManName = [];
            item.backActInfo = [];//退回节点信息
            //判断是否需要请求http获取数据
            getBackNode(procId,actId).then((result) => {
                if(result.status == "200"){
                    //判断是否有退回节点
                    let arr = result.data.data.data;
                    if(arr.length > 0){
                        //存在多个回退节点的情况
                        for(let i = 0; i < arr.length; i++){
                            let obj = arr[i];
                            item.backAct.push(obj.next_act_metadata);
                            item.backActName.push(obj.next_act_name);
                            _this.getBackNodeInfo(item,obj.next_act_id);
                            _this.getBackMan(obj,item);
                        }
                        item.showBack = true;
                    }else{
                        //说明没有退回节点
                    }
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        /**
         * 获取退回节点信息 并保存
         */
        getBackNodeInfo:function(item,actId){
            let _this = this;
            getBpmPscActInfo({actId:actId}).then((result) => {
                if(result.status == "200" && result.data.code == "0"){
                    let data = result.data.data.data;
                    item.backActInfo.push(data);
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        /**
         * 获取退回环节当时的处理人 通过日志来找到。
         * @param {*} obj
         * @param {*} item
         */
        getBackMan:function(obj,item){
            //obj 为回退的环节
            item.showProcLogs.filter(o=>{
                if(o.actName.indexOf(obj.next_act_metadata) > -1){
                    item.backMan.push(o.approverAccount);
                    item.backManName.push(o.approverChsName);
                }
            });
        },
        /**
         * 获取下一环节和下一处理人开始
         * @param {待办数据 } item
         */
        // getNextDealInfo:function(item){
        //     let _this = this;
        //     let params = {
        //         actId: item.activityID,
        //         procId: item.procID,
        //         procInstId: item.procInstID,
        //         node: (item.activityName.split('_'))[0],
        //     }
        //     //通过下一步点处理人获取相关信息(得到下一环节以及环节的处理人
        //     //这里的环节和处理人信息需要获取更详细的信息)
        //     getNextApprovePerson(params).then((result) => {
        //         if(result.status == "200" && result.data.code == "0"){
        //             let data = result.data.data.data;
        //             for(let actMetadata in data){
        //                //处理下一环节以及下一步处理人
        //                this.getNextActAndDealer(actMetadata,data,item);
        //             }
        //         }else{
        //             _this.$alert(result.data.msg,_this.$t('cm.tips'));
        //         }
        //     }).catch((err) => {
        //         _this.$alert(err,_this.$t('cm.tips'));
        //     });
        // },
        /**
         * 获取下一环节和下一处理人开始
         * @param {待办数据 } item
         */
        getNextDealInfo:function(item){
            let _this = this;
            let params = {
                actId: item.activityID,
                procId: item.procID,
                procInstId: item.procInstID,
                node: (item.activityName.split('_'))[0],
            }
            //通过下一步点处理人获取相关信息(得到下一环节以及环节的处理人
            //这里的环节和处理人信息需要获取更详细的信息)
            getNextApprovePerson(params).then((result) => {
                if(result.status == "200" && result.data.code == "0"){
                    let data = result.data.data.data;
                    for(let actMetadata in data){
                       //处理下一环节以及下一步处理人
                       this.getNextActAndDealer(actMetadata,data,item);
                    }
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        //处理下一环节以及下一步处理人
        //actMetadata (020、030这样的数据)
        //dealer (020 、030 环节的处理人 --作为下一步处理人)
        //item (流程的其他信息 --查询待办)
        getNextActAndDealer:function(actMetadata,dealer,item){
            //遍历节点信息 获得节点id(actId)
            let _this = this;
            let params = {
                fullName: item.processFullName,
                procSetId: this.$store.state.proc.procSetId
            };
            getWFConfigActivity(params).then((result) => {
                if(result.status == "200" && result.data.code == "0"){
                    //存在多个下一环节的情况
                    let d = result.data.data.data;
                    for(let i = 0; i < d.length; i++){
                        if(d[i].actMetaData == actMetadata){
                            //获取节点详细信息(020) 保存到item中
                            _this.getNodeDetails(d[i],actMetadata,dealer,item);
                        }
                        if(actMetadata == "END"){
                            item.isEnd = true
                        }
                    }
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        /**
         * 获取节点信息,通过节点id 设置视图上的下一环节和下一步处理人
         * @param {节点id} act
         * @param {020、030} actMetadata
         * @param {下一步点处理人信息} dealer
         * @param {待办信息} item
         */
        getNodeDetails:function(act,actMetadata,dealer,item){
            let _this = this;
            getBpmPscActInfo({actId:act.actID}).then((result) => {
                if(result.status == "200" && result.data.code == "0"){
                    let data = result.data.data.data;
                    item.actOptions.push(data);//设置当前下拉框的环节选项
                    item.nextActOptions.push(data);//下一环节选项(审批通过的)
                    if(item.next_actor == undefined){
                        item.next_actor = data.actMetadata;//默认选择一个为下一环节
                    }
                    let dealerArr = dealer[actMetadata];
                    _this.unifyUser(dealerArr,item);//设置下一步处理人
                    item.isReselFastApprove = data.isReselFastDealer == 1?true:false;//设置是否重选快速审批
                    item.isFastApprove = data.isFastDeal == 1 ? true:false;//设置是否快速审批
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        /**
         * 统一用户格式
         * @param {下一处理人} users
         * @param {待办信息} item
         */
        unifyUser:function(users,item){
            for(let i = 0; i < users.length; i++){
                if(typeof(users[i].item_principal_desc) != "undefined" && typeof(users[i].item_principal_desc) != null){
                    item.personOptions.push("["+users[i].item_principal+"]"+users[i].item_principal_desc);//当前视图人员选项
                    item.approvePerson.push("["+users[i].item_principal+"]"+users[i].item_principal_desc);//下一环节人员选项(审批通过)
                }
            }
            // item.nextPersonOptions.push(item.approvePerson);
        },

        /**
         * 展示流程图
         * @param {*} row
         */
        showProcPic:function(row){
            //展示流程图
            window.open(this.procPicUrl+row.procInstID);
        }
    },
    mounted() {
        var _this = this;
        _this.tables = [];
        let params = {
            pageIndex:1,
            pageSize:10,
            searchType:0,
            type:1,
        }
        this.getNeedDealList(params);

    },

}
